package com.android.grafika;

import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by android on 8/9/17.
 */

public class FileUtils {

    public static String getVideoPath(String folder) {
        return getVideoFile(folder).getPath();
    }

    public static File getVideoFile(String folder) {
        String name = generateVideoFileName();
        String sd = Environment.getExternalStorageDirectory().getPath();
        String path = sd + "/Grafika/" + folder + "/" + name + ".mp4";
        File file = new File(path);
        makeSureFileExist(file);
        return file;
    }

    private static String generateVideoFileName() {
        long dateTaken = System.currentTimeMillis();
        Date date = new Date(dateTaken);
        SimpleDateFormat dateFormat = new SimpleDateFormat("'Video_'" + "yyyyMMdd_HHmmss", Locale.US);
        return dateFormat.format(date);
    }

    /**
     * 判断文件是否存在，不存在则创建
     */
    public static boolean makeSureFileExist(File file) {
        if (file == null) {
            return false;
        }

        if (!file.isFile()) {
            final File parentFile = file.getParentFile();
            if (parentFile != null && !parentFile.isDirectory()) {
                parentFile.mkdirs();
            }

            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return file.isFile();
    }
}
